/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.entity;

import java.util.Random;
import Engine2D.Game;
import Engine2D.level.Level;
import Engine2D.level.tile.Tile;

/**
 *
 * @author Garys
 */
public class Entity {

    /**
     * 
     */
    protected final Random random = new Random();
    /**
     * 
     */
    /**
     * 
     */
    public int x, y;
    /**
     * 
     */
    public int xr = 24;
    /**
     * 
     */
    public int yu = 3;
    /**
     * 
     */
    public int xl = 8;
    /**
     * 
     */
    public int yd = 28;
    /**
     * 
     */
    public boolean removed;
    /**
     * 
     */
    public Level level;
    /**
     * 
     */
    public boolean isSwimming;

    /**
     * 
     * @param ticks
     */
    public void tick(int ticks) {
    }

    /**
     * 
     */
    public void render() {
    }

    /**
     * 
     */
    public void remove() {
        removed = true;
    }

    /**
     * 
     * @param level
     */
    public void init(Level level) {
    }

    /**
     * 
     * @param xa
     * @param ya
     * @return
     */
    public boolean move(int xa, int ya) {
        if (xa != 0 || ya != 0) {
            boolean stopped = true;
            if (xa != 0 && move2(xa, 0)) {
                stopped = false;
            }
            if (ya != 0 && move2(0, ya)) {
                stopped = false;
            }
            if (!stopped) {
                int xt = x >> 5;
                int yt = y >> 5;
                level.getTile(xt, yt).steppedOn();
            }
            return !stopped;
        }
        return true;
    }

    /**
     * 
     * @param xa
     * @param ya
     * @return
     */
    public boolean move2(int xa, int ya) {
        if (xa != 0 && ya != 0) {
            throw new IllegalArgumentException("Move2 can only move along one axis at a time!");
        }

        int xtol = ((x) + xl) >> 5;
        int ytou = ((y) + yu) >> 5;
        int xtor = ((x) + xr) >> 5;
        int ytod = ((y) + yd) >> 5;

        int xtl = ((x + xa) + xl) >> 5;
        int ytu = ((y + ya) + yu) >> 5;
        int xtr = ((x + xa) + xr) >> 5;
        int ytd = ((y + ya) + yd) >> 5;


        boolean blocked = false;
        for (int yt = ytu; yt <= ytd; yt++) {
            for (int xt = xtl; xt <= xtr; xt++) {
                if (xt >= xtol && xt <= xtor && yt >= ytou && yt <= ytod) {
                    continue;
                }

                level.getTile(xt, yt).bumped(level, xt, yt, this);
                if (!level.getTile(xt, yt).mayPass(level, xt, yt, this)) {
                    blocked = true;
                    return false;
                }
            }


        }
        if (blocked) {
            return false;
        }




        x += xa;
        y += ya;
        return true;
    }

    /**
     * 
     * @return
     */
    public int[] getCurrentTile() {
        int position[] = new int[2];
        position[0] = (x + 16) >> 5;
        position[1] = (y + 16) >> 5;
        return position;

    }

    /**
     * 
     * @param x0
     * @param y0
     * @param x1
     * @param y1
     * @return
     */
    public boolean intersects(int x0, int y0, int x1, int y1) {
        return !(x + xr < x0 || y + yu < y0 || x - xr > x1 || y - yd > y1);
    }

    /**
     * 
     * @return
     */
    public boolean canSwim() {
        return false;
    }

    /**
     * 
     * @return
     */
    public int getLightRadius() {
        return 5;
    }

    /**
     * 
     * @param xScroll
     * @param yScroll
     */
    public void render(int xScroll, int yScroll) {
        
    }

}
